﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace LevelEditor.View.Controls.Adorners
{
    public class GridAdorner : Adorner
    {
        // Fields
        private DrawingBrush gridPatternBrush;
        private GuidelineSet guidelines;
        public Rect rect;
        private Pen renderPen;
        private int squareSize;

        // Methods
        public GridAdorner(UIElement adornedElement)
            : base(adornedElement)
        {
            base.IsHitTestVisible = false;
            this.InitDrawingBrush();
        }

        private void InitDrawingBrush()
        {
            PolyLineSegment backgroundLine1 = new PolyLineSegment
            {
                Points = { new Point(1.0, 0.0), new Point(1.0, (double)this.squareSize), new Point(0.0, (double)this.squareSize) }
            };
            PathFigure line1Figure = new PathFigure
            {
                Segments = { backgroundLine1 }
            };
            PathGeometry backgroundLine1Geometry = new PathGeometry
            {
                Figures = { line1Figure }
            };
            GeometryDrawing backgroundLineDrawing1 = new GeometryDrawing(new SolidColorBrush(Color.FromArgb(0xff, 130, 130, 130)), null, backgroundLine1Geometry);
            PolyLineSegment backgroundLine2 = new PolyLineSegment
            {
                Points = { new Point(0.0, 1.0), new Point((double)this.squareSize, 1.0), new Point((double)this.squareSize, 0.0) }
            };
            PathFigure line2Figure = new PathFigure
            {
                Segments = { backgroundLine2 }
            };
            PathGeometry backgroundLine2Geometry = new PathGeometry
            {
                Figures = { line2Figure }
            };
            GeometryDrawing backgroundLineDrawing2 = new GeometryDrawing(new SolidColorBrush(Color.FromArgb(0xff, 130, 130, 130)), null, backgroundLine2Geometry);
            DrawingGroup backgroundGroup = new DrawingGroup
            {
                Children = { backgroundLineDrawing1, backgroundLineDrawing2 }
            };
            this.gridPatternBrush = new DrawingBrush(backgroundGroup);
            this.gridPatternBrush.Viewport = new Rect(0.0, 0.0, (double)this.squareSize, (double)this.squareSize);
            this.gridPatternBrush.ViewportUnits = BrushMappingMode.Absolute;
            this.gridPatternBrush.TileMode = TileMode.Tile;
            this.gridPatternBrush.Freeze();
            base.InvalidateVisual();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            if (this.SquareSize != 0)
            {
                this.rect = new Rect(base.AdornedElement.DesiredSize);
                drawingContext.DrawRectangle(this.gridPatternBrush, null, this.rect);
            }
        }

        // Properties
        public int SquareSize
        {
            get
            {
                return this.squareSize;
            }
            set
            {
                this.squareSize = value;
                this.InitDrawingBrush();
            }
        }
    }


}
